//
//  GYMineBuyInfoEditViewModel.m
//  GYDemo
//
//  Created by mac on 2018/3/28.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineBuyInfoEditViewModel.h"
#import "Head.h"

@implementation GYMineBuyInfoEditDetailCellModel
#pragma mark - getter and setter
-(BOOL)isEditable
{
    return _action ? NO : YES;
}
-(instancetype)init
{
    if (self = [super init]) {
        _cellHeight = WFCGFloatY(45);
    }
    return self;
}
-(GYMineBuyInfoEditDetailCellModel *)initWithTitle:(NSString *)title key:(NSString *)key action:(NSString *)action
{
    if (self = [super init]) {
        self.title = title;
        self.key = key;
        self.isEditable = NO;
        self.action = action;
        self.cellHeight = WFCGFloatY(45);
    }
    return self;
}
-(GYMineBuyInfoEditDetailCellModel *)initWithTitle:(NSString *)title key:(NSString *)key action:(NSString *)action other:(NSDictionary *)other
{
    if (self = [super init]) {
        self.title = title;
        self.key = key;
        self.isEditable = NO;
        self.action = action;
        self.other = other;
        self.cellHeight = WFCGFloatY(45);
    }
    return self;
}
-(GYMineBuyInfoEditDetailCellModel *)initWithTitle:(NSString *)title key:(NSString *)key keyboardType:(UIKeyboardType)inputType
{
    if (self = [super init]) {
        self.title = title;
        self.key = key;
        self.isEditable = YES;
        self.inputType = inputType;
        self.cellHeight = WFCGFloatY(45);
    }
    return self;
}
@end

@interface GYMineBuyInfoEditUpdateModel()
@property (nonatomic,strong) NSDictionary *titleInfo;
@end

@implementation GYMineBuyInfoEditUpdateModel
#pragma mark - getter and setter
-(NSMutableDictionary *)otherInfo
{
    if (!_otherInfo) {
        _otherInfo = [NSMutableDictionary new];
    }
    return _otherInfo;
}
-(NSMutableDictionary *)exportParameter
{
    NSMutableDictionary *para = [[NSMutableDictionary alloc]initWithDictionary:self.otherInfo];
    for (NSString *key in mustFillKeys) {
        NSString *value = [self valueForKey:key] ?: @"";
        if (!value || value.length <= 0) {
            NSLog(@"必选项校验");
            NSLog(@"key : %@ value : %@ class: %@",key,value,NSStringFromClass(value.class));
            para = nil;
            NSString *title = _titleInfo[key];
            if (title) {
                NSString *msg = [NSString stringWithFormat:@"请完善 %@ 信息！",title];
                [FTIndicator showErrorWithMessage:msg];
            }
            break;
        }else{
            [para setObject:value forKey:key];
        }
    }
    if (_idField) {
        [para setObject:_idField forKey:@"id"];
    }
    if (_descriptionField) {
        [para setObject:_descriptionField forKey:@"description"];
    }
    
    return para;
}
-(instancetype)init
{
    if (self = [super init]) {
        mustFillKeys = @[@"name",@"protypeid",@"province",@"city",@"area",@"unit",@"maxprice",@"minprice",@"count",@"endTimteStr"];
        _titleInfo = @{
                       @"name":@"标题",
                       @"protypeid":@"种类",
                       @"province":@"地区",
                       @"city":@"地区",
                       @"area":@"地区",
                       @"unit":@"种类",
                       @"maxprice":@"价格区间",
                       @"minprice":@"价格区间",
                       @"count":@"数量",
                       @"endTimteStr":@"有效期"
                       };
    }
    return self;
}
@end

@interface GYMineBuyInfoEditViewModel()
@property (nonatomic,assign) BOOL isUpdate;
@property (nonatomic,strong) NSMutableArray *detailNormalItems;
@property (nonatomic,strong) NSMutableArray *detailSpecialItems;
@property (nonatomic,strong) NSMutableArray *detailNormalItems2;
@end

@implementation GYMineBuyInfoEditViewModel
#pragma mark - getter and setter
-(GYMineBuyInfoEditUpdateModel *)updateModel
{
    if (!_updateModel) {
        _updateModel = [GYMineBuyInfoEditUpdateModel new];
    }
    return _updateModel;
}
-(NSMutableArray *)detailItems
{
    if (!_detailItems) {
        _detailItems = [NSMutableArray new];
    }
    [_detailItems removeAllObjects];
    [_detailItems addObject:self.detailNormalItems];
    !self.detailSpecialItems ?: [_detailItems addObject:self.detailSpecialItems];
    [_detailItems addObject:self.detailNormalItems2];
    return _detailItems;
}
-(BOOL)isUpdate
{
    _isUpdate = _buyInfoDetailModel ? YES : NO;
    return _isUpdate;
}
-(NSMutableArray *)detailNormalItems
{
    if (!_detailNormalItems) {
        _detailNormalItems = [NSMutableArray new];
        GYMineBuyInfoEditDetailCellModel *cellmodel1 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel1.title = @"标题";
        cellmodel1.key = @"name";
        cellmodel1.value = self.name;
        cellmodel1.inputType = UIKeyboardTypeDefault;
        
        GYMineBuyInfoEditDetailCellModel *cellmodel2 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel2.title = @"种类";
        cellmodel2.key = @"catogery";
        cellmodel2.value = self.catogery;
        cellmodel2.action = @"chooseCatogery:";
        
        GYMineBuyInfoEditDetailCellModel *cellmodel3 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel3.title = @"数量";
        cellmodel3.key = @"count";
        cellmodel3.value = self.count;
        cellmodel3.inputType = UIKeyboardTypeNumberPad;
        
        GYMineBuyInfoEditDetailCellModel *cellmodel4 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel4.title = @"地区";
        cellmodel4.key = @"area";
        cellmodel4.value = self.area;
        cellmodel4.action = @"chooseArea:";
        
        GYMineBuyInfoEditDetailCellModel *cellmodel5 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel5.title = @"价格";
        cellmodel5.key = @"price";
        cellmodel5.value = self.price;
        cellmodel5.action = @"inputPrice:";
        
        GYMineBuyInfoEditDetailCellModel *cellmodel6 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel6.title = @"有效期";
        cellmodel6.key = @"valid";
        cellmodel6.value = self.valid;
        cellmodel6.action = @"chooseValid:";
        
        NSArray *items = @[
          cellmodel1,
          cellmodel2,
          cellmodel3,
          cellmodel4,
          cellmodel5,
          cellmodel6,
          ];
        [_detailNormalItems addObjectsFromArray:items];
    }
    return _detailNormalItems;
}
-(NSMutableArray *)detailSpecialItems
{
    if (!_detailSpecialItems) {
        _detailSpecialItems = [NSMutableArray new];
    }
    return _detailSpecialItems;
}
-(NSMutableArray *)detailNormalItems2
{
    if (!_detailNormalItems2) {
        _detailNormalItems2 = [NSMutableArray new];
        GYMineBuyInfoEditDetailCellModel *cellmodel1 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel1.title = @"产品描述";
        cellmodel1.key = @"";
        cellmodel1.action = @"";
        
        GYMineBuyInfoEditDetailCellModel *cellmodel2 = [GYMineBuyInfoEditDetailCellModel new];
        cellmodel2.title = @"";
        cellmodel2.key = @"descriptionField";
        cellmodel2.value = self.desc;
        cellmodel2.inputType = UIKeyboardTypeDefault;
        NSArray *items = @[
                           cellmodel1,
                           cellmodel2
                           ];
        [_detailNormalItems2 addObjectsFromArray:items];
    }
    return _detailNormalItems2;
}
///
-(NSString *)name
{
    if (!_name) {
        _name = _buyInfoDetailModel.name;
    }
    return _name;
}

-(NSString *)catogery
{
    if (!_catogery) {
        _catogery = _buyInfoDetailModel.protypename;
    }
    return _catogery;
}
-(NSString *)count
{
    if (!_count) {
        if (_buyInfoDetailModel.count > 0) {
            _count = [NSString stringWithFormat:@"%ld",_buyInfoDetailModel.count];
        }
    }
    return _count;
}
-(NSString *)area
{
    if (!_area) {
        _area = _buyInfoDetailModel.buyaddress;
    }
    return _area;
}
-(NSString *)price
{
    if (!_price) {
        if (_buyInfoDetailModel.minprice>0)
        {
            _price = [NSString stringWithFormat:@"%ld - %ld元",(long)_buyInfoDetailModel.minprice,(long)_buyInfoDetailModel.maxprice];
        }
    }
    return _price;
}
-(NSString *)valid
{
    if (!_valid) {
        if (_buyInfoDetailModel.endtime) {
            NSDateFormatter *format = [[NSDateFormatter alloc] init];
            [format setDateFormat:@"yyyy-MM-dd"];
            NSDate *endTime = [format dateFromString:_buyInfoDetailModel.endtime];
            NSInteger days = [[NSDate date] distanceInDaysToDate:endTime];
            NSString *mouths = [NSString stringWithFormat:@"%ld",days / 30 + 1];
            _valid = [NSString stringWithFormat:@"%@个月",mouths];
        }else{
            _valid = @"";
        }
    }
    return _valid;
}
-(NSString *)desc
{
    if (!_desc) {
        _desc = _buyInfoDetailModel.descriptionField;
    }
    return _desc;
}
-(NSString *)unit
{
    if (!_unit) {
        _unit = _buyInfoDetailModel.unit;
    }
    return _unit;
}

-(void)setBuyInfoDetailModel:(GYMineBuyInfoDetailModel *)buyInfoDetailModel
{
    _buyInfoDetailModel = buyInfoDetailModel;
    self.updateModel.idField = [NSString stringWithFormat:@"%ld",_buyInfoDetailModel.idField];
    _updateModel.name = _buyInfoDetailModel.name;
    _updateModel.protypeid = [NSString stringWithFormat:@"%ld",_buyInfoDetailModel.protypeid];
    _updateModel.province = _buyInfoDetailModel.province;
    _updateModel.city = _buyInfoDetailModel.city;
    _updateModel.area = _buyInfoDetailModel.area;
    _updateModel.unit = _buyInfoDetailModel.unit;
    _updateModel.minprice = [NSString stringWithFormat:@"%.2ld",(long)_buyInfoDetailModel.minprice];
    _updateModel.maxprice = [NSString stringWithFormat:@"%.2ld",(long)_buyInfoDetailModel.maxprice];
    _updateModel.count = [NSString stringWithFormat:@"%ld",_buyInfoDetailModel.count];
    _updateModel.setnum = @" ";
    _updateModel.descriptionField = _buyInfoDetailModel.descriptionField;
    _updateModel.endTimteStr = _buyInfoDetailModel.endtime;
    
    //请求产品类型信息
    NSString *pid = [NSString stringWithFormat:@"%ld",_buyInfoDetailModel.protypeid];
    NSMutableDictionary *para = [NSMutableDictionary new];
    [para setObject:pid forKey:@"id"];
    [self requestType:para];
}
-(NSDictionary *)propertyInfo:(NSString *)key
{
    NSDictionary *res = nil;
    NSArray *otherProperty = _buyInfoDetailModel.specialproperty;
    if ([otherProperty isKindOfClass:[NSArray class]]) {
        if (otherProperty.count > 0) {
            for (NSDictionary *dic in otherProperty) {
                NSString *propertyid = [NSString stringWithFormat:@"%@",dic[@"propertyid"]];
                if ([key isEqualToString:propertyid]) {
                    res = dic;
                    break;
                }
            }
        }
    }
    return res;
}
-(instancetype)init
{
    if (self = [super init]) {
        //添加接口特殊处理
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"productTypeProperty"];
    }
    return self;
}
#pragma mark - method
///更新求购信息详情
-(void)upadteBuyInfoDetail
{
//    GYMineBuyInfoDetailModel *detail = [_buyInfoDetailModel yy_modelCopy];
    _buyInfoDetailModel.name = _updateModel.name;
    _buyInfoDetailModel.protypeid = _updateModel.protypeid.integerValue;
    _buyInfoDetailModel.province = _updateModel.province;
    _buyInfoDetailModel.city = _updateModel.city;
    _buyInfoDetailModel.area = _updateModel.area;
    _buyInfoDetailModel.unit = _updateModel.unit;
    _buyInfoDetailModel.maxprice = _updateModel.maxprice.doubleValue;
    _buyInfoDetailModel.minprice = _updateModel.minprice.doubleValue;
    _buyInfoDetailModel.count = _updateModel.count.integerValue;
    _buyInfoDetailModel.descriptionField = _updateModel.descriptionField;
    _buyInfoDetailModel.endtime = _updateModel.endTimteStr;
    
    NSMutableArray *otherProperty = [[NSMutableArray alloc]initWithArray:_buyInfoDetailModel.specialproperty];
    for (NSInteger i = 0; i < otherProperty.count; i++) {
        NSDictionary *dic = otherProperty[i];
        NSMutableDictionary *mdic = [[NSMutableDictionary alloc]initWithDictionary:dic];
        NSString *key = [NSString stringWithFormat:@"%@",dic[@"propertyid"]];
        NSString *value = _updateModel.otherInfo[key] ?: @"";
        [mdic setValue:value forKey:@"propertyvalue"];
        [otherProperty replaceObjectAtIndex:i withObject:mdic];
    }
    NSLog(@"%@",otherProperty);
    _buyInfoDetailModel.specialproperty = otherProperty;
    [self setUpdateSign:@"Ture"];
}
///请求商品类型特殊属性
-(void)requestType:(NSMutableDictionary *)para
{
    @WeakSelf;
    NSString *interface = @"productTypeProperty";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSArray *data = (NSArray *)infoModel.data;
        if ([data isKindOfClass:[NSArray class]]) {
            NSMutableArray *marr = [NSMutableArray new];
            for (NSDictionary *dic in data) {
                if ([dic isKindOfClass:[NSDictionary class]]) {
                    GYMineBuyInfoDetailSpecialPropertyModel *property = [GYMineBuyInfoDetailSpecialPropertyModel yy_modelWithJSON:dic];
                    NSString *title = property.name;
                    NSInteger type = property.type;
                    NSString *key = [NSString stringWithFormat:@"%ld",property.idField];
                    GYMineBuyInfoEditDetailCellModel *cellmodel;
                    if (2 == type) {
                        //选择类型
                        NSDictionary *data = @{@"data":property.list};
                        cellmodel = [[GYMineBuyInfoEditDetailCellModel alloc]initWithTitle:title key:key action:@"otherProperty:" other:data];
                        NSDictionary *defaultpro = [selfp propertyInfo:key];
                        if (defaultpro) {
                            NSString *propertyvalue = defaultpro[@"propertyvalue"] ?: @"";
                            cellmodel.value = propertyvalue;
                            [selfp.updateModel.otherInfo setValue:cellmodel.value forKey:key];
                        }else{
                            if (property.list.count > 0) {
                                NSDictionary *dic = property.list[0];
                                if ([dic isKindOfClass:[NSDictionary class]]) {
                                    NSString *value = dic[@"provalue"];
                                    cellmodel.value = value;
                                    [selfp.updateModel.otherInfo setValue:cellmodel.value forKey:key];
                                }
                            }
                        }
                    } else {
                        //输入类型
                        cellmodel = [[GYMineBuyInfoEditDetailCellModel alloc]initWithTitle:title key:key keyboardType:UIKeyboardTypeDefault];
                        NSDictionary *defaultpro = [selfp propertyInfo:key];
                        if (defaultpro) {
                            NSString *propertyvalue = defaultpro[@"propertyvalue"] ?: @"";
                            cellmodel.value = propertyvalue;
                            [selfp.updateModel.otherInfo setValue:cellmodel.value forKey:key];
                        }else{
                            cellmodel.value = @"";
                            [selfp.updateModel.otherInfo setValue:cellmodel.value forKey:key];
                        }
                        [selfp.updateModel setValue:cellmodel.value forKey:key];
                    }
                    [marr addObject:cellmodel];
                }
            }
            [selfp setDetailSpecialItems:marr];
        }
    }];
}
///保存或提交求购信息
-(void)requestSave
{
    @WeakSelf;
    NSString *interface = @"modifyBuyProduct";
    if (self.isUpdate) {
        interface = @"modifyBuyProduct";
    }else{
        interface = @"saveBuyProduct";
    }
    NSMutableDictionary *para = [self.updateModel exportParameter];
    if (para) {
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            [FTIndicator showSuccessWithMessage:@"保存成功"];
            [selfp upadteBuyInfoDetail];
        }];
    }
}
@end
